package com.mspbots.core.receivers;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mspbots.core.ticketai.model.TicketReject;
import com.mspbots.core.ticketai.model.TriggerEvent;
import com.mspbots.core.wise.controller.params.UserMappingParams;
import com.mspbots.core.wise.mapper.UsersMapper;
import com.mspbots.core.wise.model.*;
import com.mspbots.core.wise.model.dto.UserMappingResponse;
import com.mspbots.core.wise.service.TenantConfigurationService;
import com.mspbots.core.wise.service.TenantService;
import com.mspbots.core.wise.service.TenantUserMappingService;
import com.mspbots.core.wise.service.TenantUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import static com.mspbots.common.TriggerEvent.CW_TICKET_REJECT;


@Slf4j
@Component
@RequiredArgsConstructor
public class TicketRejectReceiver extends BaseReceiver {

    private final TenantConfigurationService configurationService;
    private final TenantService tenantService;
    private final TenantUserService userService;
    private final TenantUserMappingService mappingService;
    private final UsersMapper usersMapper;

    @RabbitListener(queues = "next-ticket.trigger.reject")
    public void rejectTicket(TicketReject reject) {
        TriggerEvent triggerEvent = new TriggerEvent();
        TenantConfiguration tenantConfiguration = configurationService.selectByKeyword(reject.getTenantId(), "mspbots.sync.wise.site");
        Tenant tenant = tenantService.getById(reject.getTenantId());
        TenantUser tenantUser = userService.getOne(Wrappers.<TenantUser>lambdaQuery().eq(TenantEntity::getTenantId, reject.getTenantId())
                .eq(TenantUser::getEmail, reject.getUsername()).last("LIMIT 1"));
        if (!ObjectUtils.isEmpty(tenantUser)) {
            UserMappingResponse userMappingResponse = mappingService.findByOne(UserMappingParams.builder().tenantId(reject.getTenantId()).identifier(tenantUser.getIdentifier()).build());
            triggerEvent.setUserInfo(super.objectMapper.convertValue(userMappingResponse, JsonNode.class));
        }

        ObjectNode objectNode = super.objectMapper.createObjectNode();
        objectNode.putPOJO("userName", reject.getUsername());
        objectNode.putPOJO("ticketId", reject.getTicketId());
        objectNode.putPOJO("reason", reject.getReason());
        log.info("content:{}", reject.getContent());
        objectNode.putPOJO("content", reject.getContent());
        objectNode.putPOJO("site", ObjectUtils.isEmpty(tenantConfiguration) ? "" : tenantConfiguration.getKeywordValue());
        objectNode.putPOJO("tenantName", ObjectUtils.isEmpty(tenant) ? "" : tenant.getShortName());
//        if (!ObjectUtils.isEmpty(tenantUser)){
        Users user = this.usersMapper.selectUserByEmail(reject);
        log.info("saveRejectTicket :userid:{}", user.getId());
        log.info("saveRejectTicket :ManagerUserId:{}", user.getManagerUserId());
        if (ObjectUtils.isEmpty(user.getManagerUserId())) {
            objectNode.putPOJO("managerUserEmail", "");
        } else {
            Users manager = this.usersMapper.selectById(user.getManagerUserId());
            log.info("saveRejectTicket :manager:{}", manager);
            objectNode.putPOJO("managerUserEmail", manager.getUsername());
        }
//        }else{
//            objectNode.putPOJO("managerUserEmail","");
//        }


        triggerEvent.setTenantId(reject.getTenantId());
        triggerEvent.setEventName(CW_TICKET_REJECT.getEvent());
        triggerEvent.setScope(CW_TICKET_REJECT.getScope());
        triggerEvent.setPayload(objectNode);
        log.info("msp.trigger.event:::RejectTicket::{}", objectNode);
        this.messagingTemplate.convertAndSend("msp.trigger.event", "", triggerEvent);
    }

}
